package com.baizhi.controller;

import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/users")
    //展示所有用户信息
    public Map show(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "3") Integer pageSize,
                                   @RequestParam(defaultValue = "") String query){
        System.out.println("进入show方法");
        List<User> users = userService.selectUser(pageNum, pageSize, query);
        PageInfo<User> pageInfo = new PageInfo<>(users, 3);
        int[] pages = pageInfo.getNavigatepageNums();
        Map map = new HashMap();
        map.put("users",users);
        map.put("pages",pages);
        return map;
    }
    //添加用户
    @PostMapping("/users")
    public Map<String,Object> addUser(@RequestBody User user){
        System.out.println("进入add方法");
        user.setCreateTime(new Date());
        userService.insertUser(user);
        Map map = new HashMap();
        map.put("status","success");
        return map;
    }
    //删除用户
    @DeleteMapping("users/{id}")
    public Map<String, Object> deleteUser(@PathVariable("id") Integer id){
        userService.deleteUserById(id);
        Map map = new HashMap();
        map.put("status","success");
        return map;
    }
    //修改用户信息
    @GetMapping("users/{id}")
    public Map<String,Object> showOne(@PathVariable("id") Integer id){
        System.out.println("进入到showOne中");
        User user = userService.selectUserById(id);
        Map map = new HashMap();
        map.put("user",user);
        return map;
    }
    @PutMapping("users")
    public Map<String,Object> updateUser(@RequestBody User user){
        System.out.println("进入到update中");
        user.setUpdateTime(new Date());
        userService.updateUser(user);
        Map map = new HashMap();
        map.put("status","success");
        return map;
    }
}
